package com.person.link.action;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.person.base.model.BaseUser;
import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.commons.utils.FlipPageInfo;
import com.person.link.model.Link;
import com.person.link.service.LinkService;
import com.person.title.model.TitleFunc;



public class LinkAction extends BaseAction {
	private Link link;
	private LinkService linkService;
	private String imagevideo;
	private String imagevideoFileName;
	private FlipPageInfo flipData =new FlipPageInfo();
	private String linkId;
	private String linkTypeName;
	private String strVideo;//用来保存媒体文件地址
	private String linkType;
	private String linkName;
	private String userDate;
	private String linkIntro;
	private TitleFunc titleFunc;
	/**
	 * 添加链接
	 * @return
	 */
	public String index(){
		return "createLink";
	}
	/**
	 * 保存链接
	 * @return
	 */
	public String saveLink(){
		
		// 获得登录用户的身份
		BaseUser user = (BaseUser) request.getSession().getAttribute(
				Constants.SESSION_USER);
		if (imagevideo != null) {
			upload();
		}
		link.setUserId(user.getUserId());
		link.setUserDate(new Date());
		
		linkService.addLink(link);
		String result=listLink();
		return result;
		
	}
	/**
	 * 链接列表
	 * @return
	 */
	public String listLink(){
		
		flipData=linkService.findAllLink(flipData);
		return "listLink";
	}
	
	/**
	 * 显示链接模块的细节内容
	 */
	public String detail(){
		
		link=linkService.findLinkByLinkId(linkId);
		titleFunc=linkService.findTitleFuncByFuncId(link.getFuncId());
		return "detail";
	}
	/**
	 * 修改链接
	 */
	public String modify(){
		
		link=linkService.findLinkByLinkId(linkId);
		titleFunc=linkService.findTitleFuncByFuncId(link.getFuncId());
		return "editLink";
	}
	/**
	 * 保存修改后的链接
	 */
	public String update(){
		
//		link=linkService.findLinkByLinkId(linkId);
		if (imagevideo != null) {
			String filePathAndName=request.getRealPath(File.separator)+Constants.LINK_DIR+
			link.getLinkTypeName();
			delFile(filePathAndName);
			upload();
		}
		link.setUserDate(new Date());
		link.setUserId(((BaseUser) request.getSession().getAttribute(
				Constants.SESSION_USER)).getUserId());
		linkService.updateLink(link);
		String result=listLink();
		return result;
	}
	/**
	 * 删除链接
	 */
	@SuppressWarnings("deprecation")
	public String delete(){
		
		link=linkService.findLinkByLinkId(linkId);
		String filePathAndName=request.getRealPath(File.separator)+Constants.LINK_DIR+
							link.getLinkTypeName();
		delFile(filePathAndName);
		linkService.deleteLink(link);
		String result=listLink();
		return result;
	}
	/**
	 * 查询
	 */
	public String linkSearch(){
		
		StringBuffer hql = new StringBuffer("select l.linkType,l.userDate,l.linkName,l.userId,l.linkUrl," +
				"l.linkIntro,l.linkId,u.userName,t.funcName,l.linkFlag from Link l,BaseUser u,TitleFunc t " +
				"where l.userId = u.userId and t.funcId = l.funcId");
		if(linkName.isEmpty() == false && !linkName.equals("")){
			hql.append(" and l.linkName like'%"+linkName+"%'");
		}
		if( !linkType.equals("3") && linkType.isEmpty() == false && !linkType.equals("") ){
			hql.append(" and l.linkType='"+linkType+"'");
		}
		
		if(userDate.isEmpty() == false && !userDate.equals("")){
			hql.append(" and DATE_FORMAT(l.userDate,'%Y-%m-%d')='"+userDate+"'");
		}
		if(!link.getFuncId().isEmpty()&& !link.getFuncId().equals("")){
			hql.append(" and l.funcId ='"+link.getFuncId()+"'");
		}
//		if(linkIntro.isEmpty() == false && !linkIntro.equals("")){
//			hql.append(" and l.linkIntro like '%"+linkIntro+"%'");
//		}
		hql.append(" order by l.funcId,l.linkType,l.userDate desc");
		flipData = linkService.findLinkByInfo(hql.toString(), flipData);
		return "searchLinkList";
	}
	/**
	 * 播放视频
	 * @return
	 */
	public String videoPlay(){
		
		
		return "playVideo";
	}
	/**
	 * 图片、视频文件上传
	 * @return
	 */
	
	public String upload() {
		String imagevideoType = getImagevideoFileName().substring(
				getImagevideoFileName().lastIndexOf("."));
		String rootDir = request.getRealPath(File.separator)
				+ Constants.LINK_DIR;
		String linkTypeName = String.valueOf(new Date().getTime());
		String imagevideoDir = rootDir + linkTypeName + imagevideoType;

		File rootFile = new File(rootDir);
		File imagevideoFile = new File(imagevideoDir);
		if (!rootFile.exists()) {
			rootFile.mkdirs();
		}

		if (imagevideoFile.exists()) {
			imagevideoFile.delete();
		}

		try {
			FileInputStream is = new FileInputStream(imagevideo);
			FileOutputStream os = new FileOutputStream(imagevideoDir);
			byte[] buffer = new byte[1024];
			int lenth = 0;
			while ((lenth = is.read(buffer)) > 0) {
				os.write(buffer);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		link.setLinkTypeName(linkTypeName + imagevideoType);
		return imagevideoDir;
	}
	/**
	  * 删除在服务器上的文件
	  * @param filePathAndName
	  */
	public void delFile(String filePathAndName) { 
		
	     try { 
	       String filePath = filePathAndName; 
	       filePath = filePath.toString(); 
	       File myDelFile = new File(filePath); 
	       myDelFile.delete(); 
	     } 
	     catch (Exception e) { 
	       System.out.println("删除文件操作出错"); 
	       e.printStackTrace(); 
	     } 
	 } 
	/**
	 * @return the link
	 */
	public Link getLink() {
		return link;
	}
	/**
	 * @param link the link to set
	 */
	public void setLink(Link link) {
		this.link = link;
	}
	/**
	 * @return the linkService
	 */
	public LinkService getLinkService() {
		return linkService;
	}
	/**
	 * @param linkService the linkService to set
	 */
	public void setLinkService(LinkService linkService) {
		this.linkService = linkService;
	}
	/**
	 * @return the imagevideo
	 */
	public String getImagevideo() {
		return imagevideo;
	}
	/**
	 * @param imagevideo the imagevideo to set
	 */
	public void setImagevideo(String imagevideo) {
		this.imagevideo = imagevideo;
	}
	/**
	 * @return the imagevideoFileName
	 */
	public String getImagevideoFileName() {
		return imagevideoFileName;
	}
	/**
	 * @param imagevideoFileName the imagevideoFileName to set
	 */
	public void setImagevideoFileName(String imagevideoFileName) {
		this.imagevideoFileName = imagevideoFileName;
	}
	/**
	 * @return the flipData
	 */
	public FlipPageInfo getFlipData() {
		return flipData;
	}
	/**
	 * @param flipData the flipData to set
	 */
	public void setFlipData(FlipPageInfo flipData) {
		this.flipData = flipData;
	}
	/**
	 * @return the linkId
	 */
	public String getLinkId() {
		return linkId;
	}
	/**
	 * @param linkId the linkId to set
	 */
	public void setLinkId(String linkId) {
		this.linkId = linkId;
	}
	/**
	 * @return the strVideo
	 */
	public String getStrVideo() {
		return strVideo;
	}
	/**
	 * @param strVideo the strVideo to set
	 */
	public void setStrVideo(String strVideo) {
		this.strVideo = strVideo;
	}
	/**
	 * @return the linkTypeName
	 */
	public String getLinkTypeName() {
		return linkTypeName;
	}
	/**
	 * @param linkTypeName the linkTypeName to set
	 */
	public void setLinkTypeName(String linkTypeName) {
		this.linkTypeName = linkTypeName;
	}
	/**
	 * @return the linkType
	 */
	public String getLinkType() {
		return linkType;
	}
	/**
	 * @param linkType the linkType to set
	 */
	public void setLinkType(String linkType) {
		this.linkType = linkType;
	}
	/**
	 * @return the linkName
	 */
	public String getLinkName() {
		return linkName;
	}
	/**
	 * @param linkName the linkName to set
	 */
	public void setLinkName(String linkName) {
		this.linkName = linkName;
	}
	/**
	 * @return the userDate
	 */
	public String getUserDate() {
		return userDate;
	}
	/**
	 * @param userDate the userDate to set
	 */
	public void setUserDate(String userDate) {
		this.userDate = userDate;
	}
	/**
	 * @return the linkIntro
	 */
	public String getLinkIntro() {
		return linkIntro;
	}
	/**
	 * @param linkIntro the linkIntro to set
	 */
	public void setLinkIntro(String linkIntro) {
		this.linkIntro = linkIntro;
	}
	/**
	 * @return the titleFunc
	 */
	public TitleFunc getTitleFunc() {
		return titleFunc;
	}
	/**
	 * @param titleFunc the titleFunc to set
	 */
	public void setTitleFunc(TitleFunc titleFunc) {
		this.titleFunc = titleFunc;
	}

}